import { ref } from "vue";
import { useRoute, onBeforeRouteUpdate } from "vue-router";

/**
 * @description 自动设置导航栏和页面标题
 * @returns
 */
const useTitle = () => {
  const route = useRoute();
  const title = ref(route.meta.title);

  document.title = title.value ?? "";
  onBeforeRouteUpdate((to, form) => {
    title.value = to.meta.title;
    document.title = to.meta.title ?? "";
  });

  return title.value;
};

export default useTitle;
